//
//  XHRemainingViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/16.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHRemainingViewController.h"
#import "XHWithdrawalViewController.h"

#import "XHIntegralDetailCell.h"

#import "Services.h"

#import <SVPullToRefresh/SVPullToRefresh.h>
static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHRemainingViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_messagesArray;
@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@property (weak, nonatomic) IBOutlet UILabel *ui_accountBalance;
@end

@implementation XHRemainingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_accountBalance.text = [NSString stringWithFormat:@"%.2f元",[self.accountBalance doubleValue]];
    self.ui_tableView.backgroundColor = [ColorManager colorWithHexString:@"F4F4F4"];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHIntegralDetailCell" bundle:nil] forCellReuseIdentifier:XHIntegralDetailCellIdentifier];
    self.page=kPageInit;
    [self.ui_tableView addInfiniteScrollingWithActionHandler:^{
        if (!self.hasNextPage)
        {
            //[PromptAction showErrorPrompt:@"暂无更多数据"];
        }
        if (self.m_messagesArray.count>=kPageSize){
            [self getUserAccount:self.page+1];
            return;
        }
        [self.ui_tableView.infiniteScrollingView stopAnimating];
    }];
    [self.ui_tableView addPullToRefreshWithActionHandler:^{
        self.page=kPageInit;
        [self getUserAccount:self.page];
    }];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.page = kPageInit;
    [self getUserAccount:self.page];
}

- (void)getUserAccount:(NSInteger)page
{
    if (!self.m_messagesArray)
    {
        [self showLoadingView];
    }
    [ServiceManager getAccountHistoryWithPageIndex:page success:^(NSDictionary *data) {
        if (page == kPageInit || self.m_messagesArray == nil)
        {
            self.m_messagesArray = [NSMutableArray array];
        }
        NSArray * datas = data[@"result"];
        if (datas.count>0)
        {
            self.page=page;
            [self.m_messagesArray addObjectsFromArray:datas];
        }
        if (self.m_messagesArray.count ==0)
        {
            self.ui_tableView.hidden = YES;
        }
        else
        {
            self.ui_tableView.hidden = NO;
        }
        [self.ui_tableView reloadData];
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [PublicMethod setFinishRefreshView:self.ui_tableView];
        [self performSelector:@selector(stopAnimate:) withObject:nil afterDelay:2.0];
        [self hideLoadingView];

    } failure:^{
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
        [self hideLoadingView];
    }];
}

- (void)stopAnimate:(NSTimer*)timer
{
    [self.ui_tableView.pullToRefreshView stopAnimating];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
    if([self.m_messagesArray isKindOfClass:[NSMutableArray class]])
    {
        rows = self.m_messagesArray.count;
    }
    else
    {
        rows = 0;
    }

    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [self integralDetailCell:indexPath];
    return cell;
}

- (XHIntegralDetailCell*)integralDetailCell:(NSIndexPath*)indexPath
{
    XHIntegralDetailCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHIntegralDetailCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary * dic = self.m_messagesArray[indexPath.row];
    if ([dic[@"operationType"]isEqualToString:@"PLUS"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"RECHARGE"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"GIFT"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"CONSUMPTION"])
    {
        cell.ui_addDele.text = @"-";
        cell.ui_getType.text = @"余额支付";
    }
    else if ([dic[@"operationType"]isEqualToString:@"CASHOUT"])
    {
        cell.ui_addDele.text = @"-";
        cell.ui_getType.text = @"提现";
    }
    else if ([dic[@"operationType"]isEqualToString:@"FROZEN_CASHOUT"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"UNFROZEN"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"MINUS"])
    {
        cell.ui_addDele.text = @"-";
        cell.ui_getType.text = @"其他";
    }
    else if ([dic[@"operationType"]isEqualToString:@"RECHARGE_RETURN"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"充值失败返回";
    }
    else if ([dic[@"operationType"]isEqualToString:@"INTEREST_SETTLEMENT"])
    {
        cell.ui_addDele.text = @"+";
        cell.ui_getType.text = @"理财收益";
    }
    
    cell.ui_time.text=[[NSDate dateWithString:dic[@"operateTime"]] stringDateYear];
    NSString * money = [NSString stringWithFormat:@"%.2f",[dic[@"changeAmount"] doubleValue]];
    if(([money rangeOfString:@"."].location !=NSNotFound))
    {
        NSArray * array = [money componentsSeparatedByString:@"."];
        NSString * str = [NSString stringWithFormat:@"%@",array[1]];
        if (str.length==2)
        {
            cell.ui_money.text = [NSString stringWithFormat:@"%@",money];
        }
        else if (str.length==1)
        {
            cell.ui_money.text = [NSString stringWithFormat:@"%@0",money];
        }
    }
    else
    {
        cell.ui_money.text = [NSString stringWithFormat:@"%@.00",money];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=60;
    return height;
}

#pragma - mark 余额提现

- (IBAction)withdrawalAction:(id)sender
{
    XHWithdrawalViewController * viewController = [[XHWithdrawalViewController alloc]init];
    viewController.title = @"余额提现";
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
